package com.bjsxt.service.impl;

import com.bjsxt.mapper.HostMapper;
import com.bjsxt.mapper.HostPowerMapper;
import com.bjsxt.pojo.Host;
import com.bjsxt.pojo.PageA;
import com.bjsxt.service.HostService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class HostServiceImpl implements HostService {
    @Autowired
    private HostMapper hostMapper;
    @Autowired
    private HostPowerMapper hostPowerMapper;
    @Override
    public PageA<Host> findHosts(Integer page, Integer rows,Host host) {
        Page pg = PageHelper.startPage(page, rows);
        List<Host> hosts = hostMapper.selectHosts(host);
        PageA<Host> pageA=new PageA<>(hosts,pg.getTotal());
        return pageA;
    }

    @Override
    public Integer changeHostStrong(Integer strong, Integer hid) {
        return hostMapper.updateHostStrong(strong, hid);
    }
    /**
     * 添加主持人和权限的方法:必须要进行事务管控
     * @param host:主持人对象
     * @return:两个结果共同作用的结果判断
     */
    @Override
    public Integer addHost(Host host) {
        //添加主持人
        Integer integer = hostMapper.insertHost(host);
        //添加权限
        Integer integer1 = hostPowerMapper.insertHostPower(host.getHid());

        if (integer>0 && integer1>0){
            return 1;
        }
        return 0;
    }

    @Override
    public Integer changeHostStatus(Integer[] hids, String status) {
        int a=0;
        try {
            for (Integer hid:hids){
                hostMapper.updateHostStatus(hid, status);
            }
            a=1;
        } catch (Exception e) {
            throw e;
        }
        return a;
    }
}
